package com.xysq.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.rockcent.model.SearchBO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Created by HDK on 2015/5/14.
 */
public class DBHelper extends SQLiteOpenHelper {
    public static final String PARENT = "parent";
    public static final int version = 1;
    public static final String DBNAME = "XYSQDB";
    public static final String SEARCH_HISTORY = "searchHistory";

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DBHelper(Context context) {
        this(context, DBNAME, null, version);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        createTable(db);
    }

    public void createTable(SQLiteDatabase db) {
        //首页热门券
        StringBuffer indexHotBuffer = new StringBuffer();
        indexHotBuffer.append("CREATE TABLE IF NOT EXISTS [" + SEARCH_HISTORY + "] (");
        indexHotBuffer.append("[name] TEXT,");
        indexHotBuffer.append("[createTime] Date)");
        db.execSQL(indexHotBuffer.toString());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public boolean insertData(String tableName, Map<String, Map<String, Object>> parentMap) {
        SQLiteDatabase db = getWritableDatabase();
        for (int i = 0; i < parentMap.size(); i++) {
            Map childMap = parentMap.get(PARENT + i);
            Iterator it = childMap.entrySet().iterator();
            ContentValues values = new ContentValues();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                if (value != null) {
                    values.put(key.toString(), value.toString());
                } else {
                    values.put(key.toString(), "");
                }
            }
            db.insert(tableName, null, values);
        }
        db.close();
        return false;
    }

    //加载历史记录
    public List<SearchBO> getSearchHistory(String tableName) {
        String sql = "select * from " + tableName + " order by createTime desc";
        List<SearchBO> listSearhchBO = new ArrayList<SearchBO>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{});
        while (cursor.moveToNext()) {
            SearchBO searchBO = new SearchBO();
            searchBO.setName(cursor.getString(cursor.getColumnIndex("name")));
            searchBO.setCreateTime(cursor.getLong(cursor.getColumnIndex("createTime")));
            listSearhchBO.add(searchBO);
        }
        db.close();
        return listSearhchBO;
    }

    //删除记录
    public void deleteDatas(String tableName, String key, String value) {
        SQLiteDatabase db = getReadableDatabase();
//        String sql = "delete from " + tableName + " where " + key + " = '" + value + "'";
//        Log.d("test", sql);
//        db.rawQuery(sql, null);
        db.delete(tableName, key + "='" + value + "'", null);
    }
}
